package myapps.ttclient.adapter;

import java.util.ArrayList;

import myapps.ttclient.struct.FileInformationStruct;

import myapps.ttclient.Helper;
import myapps.ttclient.R;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class PPTOpenFileArrayAdapter extends ArrayAdapter<FileInformationStruct> {
	private final Context context;
	private final ArrayList<FileInformationStruct> fileInfos;

	static class ViewHolder {
		public TextView text;
		public ImageView image;
	}

	public PPTOpenFileArrayAdapter(Context context, 
			int textViewResourceID, ArrayList<FileInformationStruct> fileInfos) {
		super(context, textViewResourceID, fileInfos);

		this.context = context;
		this.fileInfos = fileInfos;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View rowView = convertView;

		if (rowView == null) {
			LayoutInflater inflater = ((Activity) context).getLayoutInflater();
			rowView = inflater.inflate(R.layout.openfile_rowlayout, null);

			ViewHolder viewHolder = new ViewHolder();
			viewHolder.text = (TextView) rowView.findViewById(R.id.label);
			viewHolder.image = (ImageView) rowView.findViewById(R.id.icon);
			rowView.setTag(viewHolder);
		}

		ViewHolder holder = (ViewHolder) rowView.getTag();

		String s = Helper.getName(fileInfos.get(position).path);
		
		holder.text.setText(s);

		int type = fileInfos.get(position).type;

		if (type == FileInformationStruct.FOLDER) {
			holder.image.setImageResource(R.drawable.ic_file_explorer_folder_opened);
		
		} else if (type == FileInformationStruct.FILE) {
			if(s.contains("pptx") == true){
				holder.image.setImageResource(R.drawable.ic_file_explorer_pptx);
			
			} else if(s.contains("ppt") == true){
				holder.image.setImageResource(R.drawable.ic_file_explorer_ppt);
			
			} else {
				holder.image.setImageResource(R.drawable.ic_file_explorer_default_file);
			
			}
		} else if (type == FileInformationStruct.FIXED)	{
			holder.image.setImageResource(R.drawable.ic_file_explorer_hard_drive);
		
		} else if (type == FileInformationStruct.CDROM) {
			holder.image.setImageResource(R.drawable.ic_file_explorer_cd_drive);
		
		} else if (type == FileInformationStruct.NETWORK_DRIVE) {
			holder.image.setImageResource(R.drawable.ic_file_explorer_hard_drive);
		
		} else if (type == FileInformationStruct.NO_ROOTDIRECTORY) {
			holder.image.setImageResource(R.drawable.ic_file_explorer_hard_drive);
		
		} else if (type == FileInformationStruct.RAM) {
			holder.image.setImageResource(R.drawable.ic_file_explorer_hard_drive);
		
		} else if (type == FileInformationStruct.REMOVABLE) {
			holder.image.setImageResource(R.drawable.ic_file_explorer_hard_drive);
		
		} else if (type == FileInformationStruct.UNKNOWN) {
			holder.image.setImageResource(R.drawable.ic_file_explorer_hard_drive);
		
		} else {
			holder.image.setImageResource(R.drawable.ic_file_explorer_hard_drive);
		
		}

		return rowView;
	}
}
